package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.persistence.CartDAO;
import org.csu.mypetstore.persistence.impl.CartDAOImpl;
import org.csu.mypetstore.service.CatalogService;
import org.csu.mypetstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;

/**
 * Created by songtie on 2015/4/27.
 */
public class UpdateCartQuantitiesServlet extends HttpServlet {

    private static final String VIEW_CART = "/WEB-INF/jsp/cart/Cart.jsp";
    private static final String ERROR = "/WEB-INF/jsp/common/Error.jsp";

    private Cart cart;

    private CartDAO cartDAO = new CartDAOImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("111");
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("222");
        HttpSession session = request.getSession();
        cart = (Cart) session.getAttribute("cart");
        String username = (String) session.getAttribute("username");

        Iterator<CartItem> cartItems = cart.getAllCartItems();
//        while (cartItems.hasNext()) {
//            CartItem cartItem = cartItems.next();
//            String itemId = cartItem.getItem().getItemId();
//            try {
//                int quantity = Integer.parseInt((String) request.getParameter(itemId));
//                System.out.println(quantity);
//                System.out.println(itemId);
//                cart.setQuantityByItemId(itemId, quantity);
//                if (quantity < 1) {
//                    cartItems.remove();
//                }
//                cartDAO.update(itemId,username,quantity);
//            } catch (Exception e) {
//                e.printStackTrace();
//                Account account = (Account)session.getAttribute("account");
//                /********************************日志******************************/
//                if(account != null){
//                    HttpServletRequest httpRequest= request;
//                    String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
//                            + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());
//
//                    LogService logService = new LogService();
//                    String logInfo = logService.logInfo(" ") + strBackUrl + " Upddate the quantities";
//                    logService.insertLogInfo(account.getUsername(), logInfo);
//                }
//                /*****************************************************************/
//                session.setAttribute("message", "The Quantities of Item must be Integer!");
//                request.getRequestDispatcher(ERROR).forward(request, response);
//            }
//        }
//        request.getRequestDispatcher(VIEW_CART).forward(request, response);
        String itemId = request.getParameter("itemId");
        String quantity = request.getParameter("quantity");
        String listPrice = request.getParameter("listPrice");
//        listPrice = listPrice.replace("$","");
//        System.out.println(Integer.parseInt(listPrice));

        CatalogService catalogService = new CatalogService();
        CartItem cartItem = new CartItem();
        Item item = catalogService.getItem(itemId);
        cartItem.setItem(item);
        cartItem.setInStock(Boolean.parseBoolean("true"));
        cartItem.setQuantity(Integer.parseInt(quantity));
        cart.addItem(item,true,cartItem.getQuantity());

        session.setAttribute("cart",cart);

        cartDAO.update(itemId,username,Integer.parseInt(quantity));

        //int total = Integer.parseInt(listPrice)*Integer.parseInt(quantity);
        //System.out.println(total);
//        response.setContentType("text/plain");
//        PrintWriter out = response.getWriter();

        //out.print(total);
    }
}
